package com.bird.user.domain

import cn.hutool.core.util.IdUtil
import com.bird.component.ddd.model.entity.AggregateRoot
import com.bird.component.ddd.model.entity.BaseEntity
import com.bird.component.lang.util.paramcheck.toCheck
import com.bird.user.domain.vo.Nickname
import com.bird.user.domain.vo.Phone
import com.bird.user.domain.vo.UserPassword

class UserEntity(
    /**
     * 用户名
     */
   var username: String,
    /**
     * 手机号
     */
    var phone: Phone? = null,
    /**
     * 密码
     */
    var password: UserPassword,

    /**
     * 昵称
     */
    var nickname: Nickname? = null,
    /**
     * 密码盐
     */
    var salt: String,
    /**
     * 用户角色集合值对象
     */
    var roles: List<Long> = listOf(),

    ): BaseEntity(id = null), AggregateRoot {
    /**
     * 生成用户id
     */
    fun generateId() {
        //使用雪花算法生成id
        id = IdUtil.getSnowflakeNextId()
    }
    /**
     * 用户实体校验
     */
    fun validate() {
        toCheck {
            notEmpty("用户名", value = username)
            notEmpty("密码", value = password)
            notEmpty("密码盐", value = salt)
        }
    }
    /**
     * 校验密码是否正确
     */
    fun checkPassword(password: String): Boolean {
        return this.password.checkPassword(password, salt)
    }

}